package models

import (
	"github.com/gin-gonic/gin"
	"time"
)

type injury struct {
	Injury_id int `gorm:"column:injury_id"`
	Player_name string `json:"player_name" gorm:"type:varchar(32)`
	Injury_time string `gorm:"column:injury_time"`
	Injury_type string `gorm:"column:injury_type"`
	Injury_position string `gorm:"column:injury_position"`
	Injury_location string `gorm:"column:injury_location"`
	Injury_level string `gorm:"column:injury_level"`
	Injury_scene string `gorm:"column:injury_scene"`
	Injury_method string `gorm:"column:injury_method"`
	Recovery_time string `gorm:"column:recovery_time"`
	Nation_doctorname string `gorm:"column:nation_doctorname"`
	Club_doctorname string `gorm:"column:club_doctorname"`
	Is_del bool `gorm:"column:is_del"`
	Update_username string `gorm:"column:update_username"`
	Update_time time.Time `gorm:"column:update_time"`
}

func GetAllInjury(injuryParam map[string]interface{}) (error, []injury, int64) {
	var injurydata []injury
	page := injuryParam["page"].(int)
	pageSize := injuryParam["limit"].(int)
	searchText := injuryParam["searchText"].(string)
	var total int64
	err := db.Table("tb_injury").Where("player_name like ? and is_del = false", "%"+searchText+"%").Order("injury_id ASC").Count(&total).Offset((page-1)*pageSize).Limit(pageSize).Find(&injurydata).Error
	return err, injurydata, total
}

func AddInjury(a *gin.Context) (error, injury) {
	var injurydata injury
	a.BindJSON(&injurydata)
	injurydata.Is_del = false
	injurydata.Update_username = "系统管理员"
	injurydata.Update_time = time.Now()
	err := db.Table("tb_injury").Select("player_name", "injury_time", "injury_type", "injury_position", "injury_location", "injury_level", "injury_scene",
		"injury_method", "recovery_time", "nation_doctorname", "club_doctorname", "is_del", "update_username", "update_time").Create(&injurydata).Error
	return err, injurydata
}

func UpdateInjury(a *gin.Context) (error, injury) {
	var injurydata injury
	a.BindJSON(&injurydata)
	injurydata.Update_username = "系统管理员"
	injurydata.Update_time = time.Now()
	err := db.Table("tb_injury").Where("injury_id = ?", injurydata.Injury_id).Updates(&injurydata).Error
	return err, injurydata
}

func DelInjury(a *gin.Context) error {
	id := a.Params.ByName("id")
	err := db.Table("tb_injury").Where("injury_id = ?", id).Update("is_del", true).Error
	return err
}
